package MiscelaneousEntities;

import java.awt.Color;
import java.awt.Graphics;

import Constructions.Construction;
import Main.SpaceGame;
import Power.PowerCore;
import World.TileType;
import World.World;

public class Explosion extends MiscEntity
{
	public int x,y;
	int currentRadius = 0, radius;
	SpaceGame main;
	int updateCnt = 2;
	
	public Explosion(int x, int y, int radius, SpaceGame main)
	{
		this.x = x;
		this.y = y;
		this.radius = radius;
		this.main = main;
	}
	
	public void update()
	{
		if(updateCnt == 0)
		{
			if(currentRadius > radius)
				main.miscManager.miscList.remove(this);
			currentRadius++;
			for(int dx = x-currentRadius;dx<=x+currentRadius;dx++)
			{
				for(int dy = y-currentRadius;dy<=y+currentRadius;dy++)
				{
					if(dx >= 0 && dx < World.worldXSize && dy >= 0 && dy < World.worldYSize)
					{
						if(main.world.tiles[dx][dy].getType() == TileType.Hull && Math.pow(dx - x-.5,2) + Math.pow(dy - y-.5, 2) <= Math.pow(radius,2))
							main.world.tiles[dx][dy].setType(TileType.Interior);
						if(Math.pow(dx - x-.5,2) + Math.pow(dy - y-.5, 2) <= Math.pow(currentRadius/3,2))
							main.world.tiles[dx][dy].setType(TileType.Space);
						if(Math.pow(dx - x-.5,2) + Math.pow(dy - y-.5, 2) <= Math.pow(currentRadius,2))
						{
							if(Math.pow(dx - x-.5,2) + Math.pow(dy - y-.5, 2) <= Math.pow(radius,2))
							{
								main.world.tiles[dx][dy].setSolid(false);
								main.world.tiles[dx][dy].setHasConstruction(false);
							
								for(int i = 0;i<main.constructionManager.constructionList.size();i++)
								{
									if(dx >= main.constructionManager.constructionList.get(i).getX() && dx < main.constructionManager.constructionList.get(i).getX()+main.constructionManager.constructionList.get(i).getWidth() && dy >= main.constructionManager.constructionList.get(i).getY() && dy < main.constructionManager.constructionList.get(i).getY()+main.constructionManager.constructionList.get(i).getHeight())
										main.constructionManager.constructionList.remove(i);
								}
								for(int i = 0;i<main.constructionManager.enemyConstructionManager.constructionList.size();i++)
								{
									if(dx >= main.constructionManager.enemyConstructionManager.constructionList.get(i).getX() && dx < main.constructionManager.enemyConstructionManager.constructionList.get(i).getX()+main.constructionManager.enemyConstructionManager.constructionList.get(i).getWidth() && dy >= main.constructionManager.enemyConstructionManager.constructionList.get(i).getY() && dy < main.constructionManager.enemyConstructionManager.constructionList.get(i).getY()+main.constructionManager.enemyConstructionManager.constructionList.get(i).getHeight())
										main.constructionManager.enemyConstructionManager.constructionList.remove(i);
								}
								for(int i = 0;i<main.aiManager.entityList.size();i++)
								{
									if(main.aiManager.entityList.get(i).getX() == dx && main.aiManager.entityList.get(i).getY() == dy)
										main.aiManager.entityList.remove(i);
								}
								for(int i = 0;i<main.powerManager.nodeList.size();i++)
								{
									if(dx >= main.powerManager.nodeList.get(i).getX() && dx < main.powerManager.nodeList.get(i).getX()+main.powerManager.nodeList.get(i).getWidth() && dy >= main.powerManager.nodeList.get(i).getY() && dy < main.powerManager.nodeList.get(i).getY()+main.powerManager.nodeList.get(i).getHeight())
									{
										if(main.powerManager.nodeList.get(i) instanceof PowerCore)
										{
											main.gameOver = true;
											main.miscManager.miscList.add(new Explosion(dx, dy, World.worldXSize, main));
										}
										main.powerManager.nodeList.remove(i);
									}
								}
								for(int i = 0;i<main.itemManager.itemList.size();i++)
								{
									if(main.itemManager.itemList.get(i).x == dx && main.itemManager.itemList.get(i).y == dy)
										main.itemManager.itemList.remove(i);
								}
							}
						}
					}
				}
			}
			updateCnt = 2;
		}
		else
			updateCnt--;
	}
	
	public void draw(Graphics g)
	{
		if(currentRadius <= 10)
		{
			for(int dx = x-currentRadius;dx<=x+currentRadius;dx++)
			{
				for(int dy = y-currentRadius;dy<=y+currentRadius;dy++)
				{
					if(Math.pow(dx - x-.5,2) + Math.pow(dy - y-.5, 2) <= Math.pow(currentRadius,2))
					{
						int distanceCoefficient = 255*(int)(Math.sqrt(Math.pow(dx - x-.5,2) + Math.pow(dy - y-.5, 2))+.5)/radius/3;
						if(distanceCoefficient <= 255 && distanceCoefficient > 0)
							g.setColor(new Color(distanceCoefficient+100,0,0));
						else
							g.setColor(new Color(255,0,0));
						if(Math.random()<.05)
							g.setColor(Color.orange);
						g.fillRect(dx*10, dy*10, 10, 10);
					}
				}
			}
		}
		else
		{
			for(int dx = x-currentRadius;dx<=x+currentRadius;dx++)
			{
				for(int dy = y-currentRadius;dy<=y+currentRadius;dy++)
				{
					if(Math.pow(dx - x-.5,2) + Math.pow(dy - y-.5, 2) <= Math.pow(currentRadius,2) && Math.pow(dx - x-.5,2) + Math.pow(dy - y-.5, 2) > Math.pow(currentRadius-10,2))
					{
						int distanceCoefficient = 255*(int)(Math.sqrt(Math.pow(dx - x-.5,2) + Math.pow(dy - y-.5, 2))+.5)/radius/3;
						if(distanceCoefficient <= 255 && distanceCoefficient > 0)
							g.setColor(new Color(distanceCoefficient+100,0,0));
						else
							g.setColor(new Color(255,0,0));
						if(Math.random()<.05)
							g.setColor(Color.orange);
						g.fillRect(dx*10, dy*10, 10, 10);
					}
				}
			}
		}
	}
}
